//
//  FirstViewController.m
//  LoveListen
//
//  Created by 王喆 on 15/6/30.
//  Copyright (c) 2015年 王喆. All rights reserved.
//

#import "FirstViewController.h"
#define kScreenSize [UIScreen mainScreen].bounds.size
#import "FirstCell.h"
#import "FirstModel.h"
#import "JHRefresh.h"

#import "FirstDetailControllerViewController.h"
//#import "Define.h"
#define kUrl @"http://app.9nali.com/935?page_id=1&device=iPhone&version=1.1.5"

@interface FirstViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
    [self creatRefreshView];
    _isRefreshing = YES;
   // self.navigationController.title = @"爱听FM";
    [self addTaskWithUrl:kUrl isRefresh:YES];
}

-(void)creatRefreshView{
 __weak typeof (self) weakSelf = self;
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isRefreshing) {
            return ;
        }
        
        weakSelf.isRefreshing = YES;
        [weakSelf addTaskWithUrl:kUrl isRefresh:NO];
        
       
    }];
    
    
 
}

-(void)endRefreshing{
    if (self.isRefreshing) {
        self.isRefreshing = NO;
        [self.tableView headerEndRefreshingWithResult:JHRefreshResultSuccess];
    }
}


-(void)endRefreshingFailure{
    if (self.isRefreshing) {
        self.isRefreshing = NO;
        [self.tableView headerEndRefreshingWithResult:JHRefreshResultFailure];
    }
}





-(void)creatTableView{
    self.dataArr = [[NSMutableArray alloc]init];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kScreenSize.width , kScreenSize.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 85;
    [self.tableView registerNib:[UINib nibWithNibName:@"FirstCell" bundle:nil] forCellReuseIdentifier:@"cellID"];
    [self.view addSubview:self.tableView];
    
    
}

-(void)addTaskWithUrl:(NSString *)url isRefresh:(BOOL)isRefresh{
    
    _manager =[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"下载成功");
        [self.dataArr removeAllObjects];
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr = dict[@"list"];
        for (NSDictionary *dict2 in arr) {
            FirstModel *model = [[FirstModel alloc] init];
            [model setValuesForKeysWithDictionary:dict2];
            [self.dataArr addObject:model];
            
        }
        
        [self.tableView reloadData];
        [self endRefreshing];
        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
        [self endRefreshingFailure];
       
        
    }];


}

#pragma mark UITableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    FirstModel *model = self.dataArr[indexPath.row];
    [cell showDataWithModel:model indexPath:indexPath];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FirstModel *model = self.dataArr[indexPath.row];
    FirstDetailControllerViewController * detail=  [[FirstDetailControllerViewController alloc] init];
    detail.uid = [NSString stringWithFormat:@"%@",model.uid];
    
    [self.navigationController pushViewController:detail animated:YES];
    
  

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
